﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsDataGrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            initGridView();
        }

        private void initGridView()
        {
            // 列的设置
            this.dataGridView1.ColumnCount = 4;
            dataGridView1.Columns[0].Name = "学号";
            dataGridView1.Columns[1].Name = "姓名";
            dataGridView1.Columns[2].Name = "性别";
            dataGridView1.Columns[3].Name = "手机号";

            // 设置某列为不可编辑
            dataGridView1.Columns[0].ReadOnly = true;

            // 指定自定义的单元格
            dataGridView1.Columns[2].CellTemplate = new MySexCell();

            AddRow(new Student(20201001, "john", true, "13810012345"));
            AddRow(new Student(20201002, "lila", true, "13810012346"));
            AddRow(new Student(20201003, "rose", false, "13810012347"));

        }

        private void AddRow(Student student)
        {
            object[] row =
            {
                student.Id,
                student.Name,
                student.Sex ? "男": "女",
                student.Phone
            };
            dataGridView1.Rows.Add(row);
        }

        // 添加一行数据
        private void add_button_Click(object sender, EventArgs e)
        {
            object[] row = new object[4];
            row[0] = 20201004;
            row[1] = "张三";
            row[2] = "男";
            row[3] = "13899990000";

            dataGridView1.Rows.Add(row);
        }

        // 获取表格的数据
        private void get_all_button_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                object id = dataGridView1[0, i].Value;
                object name = dataGridView1[1, i].Value;
                object sex = dataGridView1.Rows[i].Cells[2].Value;
                object phone = dataGridView1.Rows[i].Cells[3].Value;

                MessageBox.Show($"{id}: {name} {sex} {phone}");
            }
        }

        
        private void del_button_Click(object sender, EventArgs e)
        {
            // 单项删除
            //dataGridView1.Rows.RemoveAt(0);


            // 多项删除
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                dataGridView1.Rows.Remove(row);
            }
        }

        // 输入验证
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                // 姓名验证
            }

            if (e.ColumnIndex == 2)
            {
                // 性别验证
                string[] sex_array = { "男", "女" };

                string sex = e.FormattedValue.ToString();
                int index = Array.IndexOf(sex_array, sex);

                Console.WriteLine($"===: {sex}: {index}");
                if (index == -1)
                {
                    MessageBox.Show("性别必须是男或女!", "Error");
                    e.Cancel = true;
                    return;
                }
            }

            if(e.ColumnIndex == 3)
            {
                // 手机号
                string str = e.FormattedValue.ToString();
                if (str.Length != 11)
                {
                    MessageBox.Show("必须为11位!", "Error");
                    e.Cancel = true;
                    return;
                }
                foreach (char ch in str)
                {
                    if (ch < '0' || ch > '9')
                    {
                        MessageBox.Show("必须为数字!", "Error");
                        e.Cancel = true;
                        return;
                    }
                }
            }
        }

        // 已经验证通过
        private void grid_CellValidated(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
